remove(list = ls())
library("dplyr")
library("plyr")
library("tidyr")
library("foreign")
library("car")
library("haven")
library("readstata13")
library("tibble")
location <- "/Users/dillonlaaker/Box Sync/Stability/"
setwd(paste(location, "data/datasets", sep = ""))

liss <- read.dta13("liss_data.dta")
bes <- read.dta13("bes_data.dta")

liss$polinformation <- liss$polinterest8 + liss$newsinterest8
liss$polinformation[liss$polinformation<2] <- 0
liss$polinformation[liss$polinformation==2] <- 1

bes$polinformation <- bes$knowledge2
bes$polinformation[bes$knowledge2<.6] <- 0
bes$polinformation[bes$knowledge2>=.6] <- 1
bes$education <- bes$education1
bes$education[bes$education1==1] <- 1
bes$education[bes$education1<1] <- 0

##Correlations by Political Sophistication
lisscor1 <- cor(liss$immigration_8[liss$education==1], liss$immigration_17[liss$education==1], method="spearman", use="pairwise")
lisscor2 <- cor(liss$immigration_8[liss$education==0], liss$immigration_17[liss$education==0], method="spearman", use="pairwise")
lisscor3 <- lisscor1-lisscor2

liss1cor <- cor(liss$imm1_8[liss$education==1], liss$imm1_17[liss$education==1], method="spearman", use="pairwise")
liss2cor <- cor(liss$imm2_8[liss$education==1], liss$imm2_17[liss$education==1], method="spearman", use="pairwise")
liss3cor <- cor(liss$imm3_8[liss$education==1], liss$imm3_17[liss$education==1], method="spearman", use="pairwise")
liss4cor <- cor(liss$imm4_8[liss$education==1], liss$imm4_17[liss$education==1], method="spearman", use="pairwise")
liss5cor <- cor(liss$imm5_8[liss$education==1], liss$imm5_17[liss$education==1], method="spearman", use="pairwise")
liss6cor <- cor(liss$imm6_8[liss$education==1], liss$imm6_17[liss$education==1], method="spearman", use="pairwise")
lisscoravg1 <- (liss1cor + liss2cor + liss3cor + liss4cor + liss5cor + liss6cor)/6
liss1cor <- cor(liss$imm1_8[liss$education==0], liss$imm1_17[liss$education==0], method="spearman", use="pairwise")
liss2cor <- cor(liss$imm2_8[liss$education==0], liss$imm2_17[liss$education==0], method="spearman", use="pairwise")
liss3cor <- cor(liss$imm3_8[liss$education==0], liss$imm3_17[liss$education==0], method="spearman", use="pairwise")
liss4cor <- cor(liss$imm4_8[liss$education==0], liss$imm4_17[liss$education==0], method="spearman", use="pairwise")
liss5cor <- cor(liss$imm5_8[liss$education==0], liss$imm5_17[liss$education==0], method="spearman", use="pairwise")
liss6cor <- cor(liss$imm6_8[liss$education==0], liss$imm6_17[liss$education==0], method="spearman", use="pairwise")
lisscoravg2 <- (liss1cor + liss2cor + liss3cor + liss4cor + liss5cor + liss6cor)/6
lisscoravg3 <- lisscoravg1-lisscoravg2

lisscor4 <- cor(liss$immigration_8[liss$polinformation==1], liss$immigration_17[liss$polinformation==1], method="spearman", use="pairwise")
lisscor5 <- cor(liss$immigration_8[liss$polinformation==0], liss$immigration_17[liss$polinformation==0], method="spearman", use="pairwise")
lisscor6 <- lisscor4-lisscor5

liss1cor <- cor(liss$imm1_8[liss$polinformation==1], liss$imm1_17[liss$polinformation==1], method="spearman", use="pairwise")
liss2cor <- cor(liss$imm2_8[liss$polinformation==1], liss$imm2_17[liss$polinformation==1], method="spearman", use="pairwise")
liss3cor <- cor(liss$imm3_8[liss$polinformation==1], liss$imm3_17[liss$polinformation==1], method="spearman", use="pairwise")
liss4cor <- cor(liss$imm4_8[liss$polinformation==1], liss$imm4_17[liss$polinformation==1], method="spearman", use="pairwise")
liss5cor <- cor(liss$imm5_8[liss$polinformation==1], liss$imm5_17[liss$polinformation==1], method="spearman", use="pairwise")
liss6cor <- cor(liss$imm6_8[liss$polinformation==1], liss$imm6_17[liss$polinformation==1], method="spearman", use="pairwise")
lisscoravg4 <- (liss1cor + liss2cor + liss3cor + liss4cor + liss5cor + liss6cor)/6
liss1cor <- cor(liss$imm1_8[liss$polinformation==0], liss$imm1_17[liss$polinformation==0], method="spearman", use="pairwise")
liss2cor <- cor(liss$imm2_8[liss$polinformation==0], liss$imm2_17[liss$polinformation==0], method="spearman", use="pairwise")
liss3cor <- cor(liss$imm3_8[liss$polinformation==0], liss$imm3_17[liss$polinformation==0], method="spearman", use="pairwise")
liss4cor <- cor(liss$imm4_8[liss$polinformation==0], liss$imm4_17[liss$polinformation==0], method="spearman", use="pairwise")
liss5cor <- cor(liss$imm5_8[liss$polinformation==0], liss$imm5_17[liss$polinformation==0], method="spearman", use="pairwise")
liss6cor <- cor(liss$imm6_8[liss$polinformation==0], liss$imm6_17[liss$polinformation==0], method="spearman", use="pairwise")
lisscoravg5 <- (liss1cor + liss2cor + liss3cor + liss4cor + liss5cor + liss6cor)/6
lisscoravg6 <- lisscoravg4-lisscoravg5

bescor1 <- cor(bes$imm1[bes$education==1], bes$imm11[bes$education==1], method="spearman", use="pairwise")
bescor2 <- cor(bes$imm1[bes$education==0], bes$imm11[bes$education==0], method="spearman", use="pairwise")
bescor3 <- bescor1-bescor2

bescorecon <- cor(bes$immecon1[bes$education==1], bes$immecon11[bes$education==1], method="spearman", use="pairwise")
bescorcul <- cor(bes$immcul1[bes$education==1], bes$immcul11[bes$education==1], method="spearman", use="pairwise")
bescorwelfare <- cor(bes$immwelfare1[bes$education==1], bes$immwelfare11[bes$education==1], method="spearman", use="pairwise")
bescoravg1 <- (bescorecon + bescorcul + bescorwelfare)/3

bescorecon <- cor(bes$immecon1[bes$education==0], bes$immecon11[bes$education==0], method="spearman", use="pairwise")
bescorcul <- cor(bes$immcul1[bes$education==0], bes$immcul11[bes$education==0], method="spearman", use="pairwise")
bescorwelfare <- cor(bes$immwelfare1[bes$education==0], bes$immwelfare11[bes$education==0], method="spearman", use="pairwise")
bescoravg2 <- (bescorecon + bescorcul + bescorwelfare)/3
bescoravg3 <- bescoravg1-bescoravg2

bescor4 <- cor(bes$imm1[bes$polinformation==1], bes$imm11[bes$polinformation==1], method="spearman", use="pairwise")
bescor5 <- cor(bes$imm1[bes$polinformation==0], bes$imm11[bes$polinformation==0], method="spearman", use="pairwise")
bescor6 <- bescor4-bescor5

bescorecon <- cor(bes$immecon1[bes$polinformation==1], bes$immecon11[bes$polinformation==1], method="spearman", use="pairwise")
bescorcul <- cor(bes$immcul1[bes$polinformation==1], bes$immcul11[bes$polinformation==1], method="spearman", use="pairwise")
bescorwelfare <- cor(bes$immwelfare1[bes$polinformation==1], bes$immwelfare11[bes$polinformation==1], method="spearman", use="pairwise")
bescoravg4 <- (bescorecon + bescorcul + bescorwelfare)/3

bescorecon <- cor(bes$immecon1[bes$polinformation==0], bes$immecon11[bes$polinformation==0], method="spearman", use="pairwise")
bescorcul <- cor(bes$immcul1[bes$polinformation==0], bes$immcul11[bes$polinformation==0], method="spearman", use="pairwise")
bescorwelfare <- cor(bes$immwelfare1[bes$polinformation==0], bes$immwelfare11[bes$polinformation==0], method="spearman", use="pairwise")
bescoravg5 <- (bescorecon + bescorcul + bescorwelfare)/3
bescoravg6 <- bescoravg4-bescoravg5


##Correlations by Age
liss$age1 <- liss$age8
liss$age1[liss$age8>=30] <- 1
liss$age1[liss$age8<30] <- 0

bes$age1 <- bes$age
bes$age1[bes$age>=30] <- 1
bes$age1[bes$age<30] <- 0

lisscor7 <- cor(liss$immigration_8[liss$age1==1], liss$immigration_17[liss$age1==1], method="spearman", use="pairwise")
lisscor8 <- cor(liss$immigration_8[liss$age1==0], liss$immigration_17[liss$age1==0], method="spearman", use="pairwise")
lisscor9 <- lisscor7-lisscor8

liss1cor <- cor(liss$imm1_8[liss$age1==1], liss$imm1_17[liss$age1==1], method="spearman", use="pairwise")
liss2cor <- cor(liss$imm2_8[liss$age1==1], liss$imm2_17[liss$age1==1], method="spearman", use="pairwise")
liss3cor <- cor(liss$imm3_8[liss$age1==1], liss$imm3_17[liss$age1==1], method="spearman", use="pairwise")
liss4cor <- cor(liss$imm4_8[liss$age1==1], liss$imm4_17[liss$age1==1], method="spearman", use="pairwise")
liss5cor <- cor(liss$imm5_8[liss$age1==1], liss$imm5_17[liss$age1==1], method="spearman", use="pairwise")
liss6cor <- cor(liss$imm6_8[liss$age1==1], liss$imm6_17[liss$age1==1], method="spearman", use="pairwise")
lisscoravg7 <- (liss1cor + liss2cor + liss3cor + liss4cor + liss5cor + liss6cor)/6

liss1cor <- cor(liss$imm1_8[liss$age1==0], liss$imm1_17[liss$age1==0], method="spearman", use="pairwise")
liss2cor <- cor(liss$imm2_8[liss$age1==0], liss$imm2_17[liss$age1==0], method="spearman", use="pairwise")
liss3cor <- cor(liss$imm3_8[liss$age1==0], liss$imm3_17[liss$age1==0], method="spearman", use="pairwise")
liss4cor <- cor(liss$imm4_8[liss$age1==0], liss$imm4_17[liss$age1==0], method="spearman", use="pairwise")
liss5cor <- cor(liss$imm5_8[liss$age1==0], liss$imm5_17[liss$age1==0], method="spearman", use="pairwise")
liss6cor <- cor(liss$imm6_8[liss$age1==0], liss$imm6_17[liss$age1==0], method="spearman", use="pairwise")
lisscoravg8 <- (liss1cor + liss2cor + liss3cor + liss4cor + liss5cor + liss6cor)/6
lisscoravg9 <- lisscoravg7-lisscoravg8

bescor7 <- cor(bes$imm1[bes$age1==1], bes$imm11[bes$age1==1], method="spearman", use="pairwise")
bescor8 <- cor(bes$imm1[bes$age1==0], bes$imm11[bes$age1==0], method="spearman", use="pairwise")
bescor9 <- bescor7-bescor8

bescorecon <- cor(bes$immecon1[bes$age1==1], bes$immecon11[bes$age1==1], method="spearman", use="pairwise")
bescorcul <- cor(bes$immcul1[bes$age1==1], bes$immcul11[bes$age1==1], method="spearman", use="pairwise")
bescorwelfare <- cor(bes$immwelfare1[bes$age1==1], bes$immwelfare11[bes$age1==1], method="spearman", use="pairwise")
bescoravg7 <- (bescorecon + bescorcul + bescorwelfare)/3

bescorecon <- cor(bes$immecon1[bes$age1==0], bes$immecon11[bes$age1==0], method="spearman", use="pairwise")
bescorcul <- cor(bes$immcul1[bes$age1==0], bes$immcul11[bes$age1==0], method="spearman", use="pairwise")
bescorwelfare <- cor(bes$immwelfare1[bes$age1==0], bes$immwelfare11[bes$age1==0], method="spearman", use="pairwise")
bescoravg8 <- (bescorecon + bescorcul + bescorwelfare)/3
bescoravg9 <- bescoravg7-bescoravg8

fix <- c("")

polsoph1 <- format(round(c(lisscor1, lisscor2, lisscor3, lisscor4, lisscor5, lisscor6, lisscor7, lisscor8, lisscor9), digits=3), nsmall = 3)
polsoph2 <- format(round(c(lisscoravg1, lisscoravg2, lisscoravg3, lisscoravg4, lisscoravg5, lisscoravg6, lisscoravg7, lisscoravg8, lisscoravg9), digits=3), nsmall = 3)
polsophd <- c(format(round(lisscor1 - lisscoravg1, digits=3), nsmall = 3), format(round(lisscor2 - lisscoravg2, digits=3), nsmall = 3), "", format(round(lisscor4 - lisscoravg4, digits=3), nsmall = 3), format(round(lisscor5 - lisscoravg5, digits=3), nsmall = 3), "", format(round(lisscor7 - lisscoravg7, digits=3), nsmall = 3), format(round(lisscor8 - lisscoravg8, digits=3), nsmall = 3), "")
table1 <- data.frame(rbind(polsoph1, polsoph2, polsophd))
table1 <- add_column(table1, new=fix, .after = "X3")
table1 <- add_column(table1, new1=fix, .after = "X6")

polsoph1 <- format(round(c(bescor1, bescor2, bescor3, bescor4, bescor5, bescor6, bescor7, bescor8, bescor9), digits=3), nsmall = 3)
polsoph2 <- format(round(c(bescoravg1, bescoravg2, bescoravg3, bescoravg4, bescoravg5, bescoravg6, bescoravg7, bescoravg8, bescoravg9), digits=3), nsmall = 3)
polsophd <- c(format(round(bescor1 - bescoravg1, digits=3), nsmall = 3), format(round(bescor2 - bescoravg2, digits=3), nsmall = 3), "", format(round(bescor4 - bescoravg4, digits=3), nsmall = 3), format(round(bescor5 - bescoravg5, digits=3), nsmall = 3), "", format(round(bescor7 - bescoravg7, digits=3), nsmall = 3), format(round(bescor8 - bescoravg8, digits=3), nsmall = 3), "")
table2 <- data.frame(rbind(polsoph1, polsoph2, polsophd))
table2 <- add_column(table2, new=fix, .after = "X3")
table2 <- add_column(table2, new1=fix, .after = "X6")

rownames(table1) <- c("Scale", "Indiv. Items", "Dif")
rownames(table2) <- c("Scale", "Indiv. Items", "Dif")
table1 <- xtable(table1, type = "latex", latex.environments = "center", caption = "")
print(table1, include.rownames=TRUE, include.colnames=FALSE,  sanitize.text.function=identity, only.contents=TRUE, hline.after = NULL, file=paste(location, "Draft/tables/table4a.tex", sep = ""))
table2 <- xtable(table2, type = "latex", latex.environments = "center", caption = "")
print(table2, include.rownames=TRUE, include.colnames=FALSE,  sanitize.text.function=identity, only.contents=TRUE, hline.after = NULL, file=paste(location, "Draft/tables/table4b.tex", sep = ""))
